package com.hksj.cbm.service;

import cn.hutool.core.util.StrUtil;
import com.hksj.cbm.dto.LargeScreenBaseDTO;
import com.hksj.cbm.mapper.LargeScreenBaseMapper;
import com.hksj.cbm.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zengke
 * @version 1.0
 * @see LargeScreenBaseService
 * @since 2024/08/20 14:06
 */

//大屏基础业务
@Service
public class LargeScreenBaseService {

    @Autowired
    private LargeScreenBaseMapper largeScreenBaseMapper;

    @Autowired
    private DeviceInstanceService deviceInstanceService;

    /**
     * 查询大屏已绑定设备模型列表
     * @param largeScreenBaseDTO 大屏基础DTO
     * @return 大屏设备模型列表
     */
    public List<LargeScreenDeviceModelVO> queryLargeScreenBindDeviceModelList(LargeScreenBaseDTO largeScreenBaseDTO){

        //查询大屏已绑定设备模型的列表
        List<LargeScreenDeviceModelVO> largeScreenDeviceModelList = largeScreenBaseMapper.queryLargeScreenBindDeviceModelList(largeScreenBaseDTO);

        //获取大屏绑定的设备id集合
        List<String> deviceIdList = largeScreenDeviceModelList.stream().map(LargeScreenDeviceModelVO::getDeviceId).collect(Collectors.toList());

        //判断设备模型id集合是否为空
        if (!ObjectUtils.isEmpty(deviceIdList)){

            //若不为空
            //通过设备id集合查询设备物模型
            Map<String, Map<String, DeriveMetadataVO>> devicePropertyMap = deviceInstanceService.batchQueryDeviceAttribute(deviceIdList);

            //则查询设备的物模型属性信息
            List<DeviceMetadataParseInfoVO> deviceMetadataParseInfoList = largeScreenBaseMapper.queryLargeScreenDevicePropertyList(deviceIdList);

            //判断设备的物模型属性列表是否为空
            if (!ObjectUtils.isEmpty(deviceMetadataParseInfoList)){

                //设置设备物模型属性的信息列表
                deviceMetadataParseInfoList.forEach(deviceMetadataParseInfoVO -> {

                    //遍历devicePropertyMap获取设备的物模型对应的名称
                    Map<String, DeriveMetadataVO> deriveMetadataMap = devicePropertyMap.get(deviceMetadataParseInfoVO.getDeviceId());
                    deviceMetadataParseInfoVO.setPropertyName(deriveMetadataMap.get(deviceMetadataParseInfoVO.getProperty()).getKeyName());
                });


                //不为空,将物模型属性列表赋值到每台设备

                //遍历设备模型列表、物模型属性列表
                largeScreenDeviceModelList.forEach(largeScreenDeviceModelVO -> {
                    deviceMetadataParseInfoList.forEach(deviceMetadataParseInfoVO -> {

                        //若物模型的设备id与当前设备模型的id相同 则将物模型数据添加到每个设备的设备物模型属性列表中
                        if (Objects.equals(deviceMetadataParseInfoVO.getDeviceId(),largeScreenDeviceModelVO.getDeviceId())){
                            largeScreenDeviceModelVO.getDeviceMetadataParseInfoList().add(deviceMetadataParseInfoVO);
                        }
                    });
                });
            }
        }

        //取出设备模型的列表中的文件id 组成文件id集合
        List<String> modelFileIdList = largeScreenDeviceModelList
                .stream()//转成stream流
                .map(LargeScreenDeviceModelVO::getModelFile)//将每个对象的modelFile属性取出,并返回
                .flatMap(modelFileId -> StrUtil.split(modelFileId, ",", -1, true, true).stream())//对modelFile属性进行扁平化处理,调用StrUtil.split进行字符串分隔,忽略空的modelFile,最后将其转换为stream流返回
                .collect(Collectors.toList());//将stream流收集,返回 modelFileIdList

        //判断文件id集合是否为空
        if (!ObjectUtils.isEmpty(modelFileIdList)){

            //若文件id集合不为空
            //通过文件id集合查询大屏文件集合
            List<LargeScreenFileVO> largeScreenFileList = largeScreenBaseMapper.queryLargeScreenFileList(modelFileIdList);

            //把文件集合设置到每个设备模型对象中
            largeScreenDeviceModelList.forEach(largeScreenDeviceModelVO -> {
                largeScreenFileList.forEach(largeScreenFileVO -> {
                    if (largeScreenDeviceModelVO.getModelFile() != null && largeScreenDeviceModelVO.getModelFile().contains(largeScreenFileVO.getId())){
                        largeScreenDeviceModelVO.getFileList().add(largeScreenFileVO);
                    }
                });
            });
        }

        //返回大屏设备模型列表
        return largeScreenDeviceModelList;
    }

    /**
     * 查询大屏虚拟设备模型列表(虚拟设备为 未挂载实体设备的设备模型)
     * @param largeScreenBaseDTO 大屏基础DTO
     * @return 大屏回路设备模型列表
     */
    public List<LargeScreenLoopDeviceModelVO> queryLargeScreenBindVirtualDeviceModelList(LargeScreenBaseDTO largeScreenBaseDTO){

        //查询大屏虚拟设备模型列表
        List<LargeScreenLoopDeviceModelVO> largeScreenLoopDeviceModelList = largeScreenBaseMapper.queryLargeScreenBindVirtualDeviceModelList(largeScreenBaseDTO);

        //取出设备模型的列表中的文件id 组成文件id集合
        List<String> modelFileIdList = largeScreenLoopDeviceModelList
                .stream()//转成stream流
                .map(LargeScreenLoopDeviceModelVO::getModelFile)//将每个对象的modelFile属性取出,并返回
                .flatMap(modelFileId -> StrUtil.split(modelFileId, ",", -1, true, true).stream())//对modelFile属性进行扁平化处理,调用StrUtil.split进行字符串分隔,忽略空的modelFile,最后将其转换为stream流返回
                .collect(Collectors.toList());//将stream流收集,返回 modelFileIdList

        //判断文件id集合是否为空
        if (!ObjectUtils.isEmpty(modelFileIdList)){

            //若文件id集合不为空
            //通过文件id集合查询大屏文件集合
            List<LargeScreenFileVO> largeScreenFileList = largeScreenBaseMapper.queryLargeScreenFileList(modelFileIdList);

            //把文件集合设置到每个设备模型对象中
            largeScreenLoopDeviceModelList.forEach(largeScreenDeviceModelVO -> {
                largeScreenFileList.forEach(largeScreenFileVO -> {
                    if (largeScreenDeviceModelVO.getModelFile() != null && largeScreenDeviceModelVO.getModelFile().contains(largeScreenFileVO.getId())){
                        largeScreenDeviceModelVO.getFileList().add(largeScreenFileVO);
                    }
                });
            });
        }

        //返回查询结果
        return largeScreenLoopDeviceModelList;
    }


    public List<LargeScreenVideoDeviceModelVO> queryLargeScreenBindVideoDeviceModelList(LargeScreenBaseDTO largeScreenBaseDTO) {
        //查询大屏视频设备模型列表
        List<LargeScreenVideoDeviceModelVO> largeScreenBindVideoDeviceModelList = largeScreenBaseMapper.queryLargeScreenBindVideoDeviceModelList(largeScreenBaseDTO);

        //取出设备模型的列表中的文件id 组成文件id集合
        List<String> modelFileIdList = largeScreenBindVideoDeviceModelList
                .stream()//转成stream流
                .map(LargeScreenVideoDeviceModelVO::getModelFile)//将每个对象的modelFile属性取出,并返回
                .flatMap(modelFileId -> StrUtil.split(modelFileId, ",", -1, true, true).stream())//对modelFile属性进行扁平化处理,调用StrUtil.split进行字符串分隔,忽略空的modelFile,最后将其转换为stream流返回
                .collect(Collectors.toList());//将stream流收集,返回 modelFileIdList

        //判断文件id集合是否为空
        if (!ObjectUtils.isEmpty(modelFileIdList)){

            //若文件id集合不为空
            //通过文件id集合查询大屏文件集合
            List<LargeScreenFileVO> largeScreenFileList = largeScreenBaseMapper.queryLargeScreenFileList(modelFileIdList);

            //把文件集合设置到每个设备模型对象中
            largeScreenBindVideoDeviceModelList.forEach(largeScreenDeviceModelVO -> {
                largeScreenFileList.forEach(largeScreenFileVO -> {
                    if (largeScreenDeviceModelVO.getModelFile() != null && largeScreenDeviceModelVO.getModelFile().contains(largeScreenFileVO.getId())){
                        largeScreenDeviceModelVO.getFileList().add(largeScreenFileVO);
                    }
                });
            });
        }

        //返回查询结果
        return largeScreenBindVideoDeviceModelList;
    }
}
